import { createContext,useState,useEffect } from 'react';

 export const WsContext = createContext(1);
 export function WsProducer({component}){
    const [ws, setWs] = useState(null);

  useEffect(() => {
    const socket = new WebSocket('ws://localhost:8000/ws');
    setWs(socket);

    // 清理 WebSocket 连接
    return () => {
      socket.close();
    };
  }, []);

  return (
    <WsContext.Provider value={ws}>
      {component}
    </WsContext.Provider>
  );

 }